<?php
/**
 * Created by PhpStorm.
 * User: sogubaby
 * Date: 2019-02-21
 * Time: 17:00
 */

namespace App\Library\Source\Rong360;


use App\Library\Guzzle\Sign;
use GuzzleHttp\Client;

class Rong360Request {

    use \App\Library\Traits\Rong360Request;
    /*
     * 获取图片内容
     * */
    public $imageMethod = 'tjy.image.api.fetch';

    /*
    * 还款计划推送
    * */
    public $pushRepayPlan = 'is.api.v3.order.pushrepayment';

    /*
    * 订单状态推送
    * */
    public $pushOrderStatus = 'is.api.v3.order.orderfeedback';

    /*
     * 绑卡反馈推送
     * */
    public $pushBindCardResult = 'is.api.v3.order.bindcardfeedback';

    /*
     * 还款结果推送
     * */
    public $pushRepayResult = 'is.api.v3.order.repayfeedback';

    /*
     * 审批结果反馈
     * */
    public $pushLoanResult = 'is.api.v3.order.approvefeedback';

    /**
     * 获取图片内容
     * @param $orderNo
     * @param $token
     * @return mixed|\Psr\Http\Message\ResponseInterface
     * @throws \App\Exceptions\GeneralException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @author luwei
     * @date 2019-02-23 13:54
     */
    public function imageInfo($orderNo, $token) {
        $data = [
            'order_no' => $orderNo,
            'token'    => $token
        ];
        return $this->request($data, $this->imageMethod);
    }

    /**
     * 推送还款计划
     * 调用场景
     * 放款完成后，调用融360接口告知还款计划
     * 用户还款后，调用融360接口更新还款计划
     * 用户逾期后，调用融360接口更新还款计划
     * 用户展期后，调用融360接口更新还款计划
     * 用户更换还款银行卡后，调用融360接口更新还款计划中的银行卡
     * 其他情况导致还款计划变更，调用融360接口更新还款计划
     * @param $orderNo 订单号（融360提交过来）
     * @param $openBank 银行名称
     * @param $bankCard 银行卡号
     * @param $repaymentPlan 还款计划 array
     * @param $canPrepay 是否支持提前结清
     * @param $canPrepayTime 可提前结清的时间
     * @return mixed|\Psr\Http\Message\ResponseInterface
     * @throws \App\Exceptions\GeneralException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @author luwei
     * @date 2019-02-23 13:59
     */
    public function pushRepayPlan($orderNo, $openBank, $bankCard, $repaymentPlan, $canPrepay, $canPrepayTime) {
        $data = [
            'order_no'        => $orderNo,
            'open_bank'       => $openBank,
            'bank_card'       => $bankCard,
            'repayment_plan'  => $repaymentPlan,
            'can_prepay'      => $canPrepay,
            'can_prepay_time' => $canPrepayTime
        ];
        return $this->request($data, $this->pushRepayPlan);
    }

    /**
     * 订单状态推送
     * @param $orderNo
     * @param $orderStatus 参考 App\Constant\Rong360Enum 订单状态
     * @param $updateTime 反馈订单流经至对应状态的时间，例如：推送170状态，此时间代表该订单实际放款时间；推送200，此时间代表该订单全部欠款还清的时间； 视具体状态而定
     * @return mixed|\Psr\Http\Message\ResponseInterface
     * @throws \App\Exceptions\GeneralException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @author luwei
     * @date 2019-02-23 14:21
     */
    public function pushOrderStatus($orderNo, $orderStatus, $updateTime) {
        $data = [
            'order_no'     => $orderNo,
            'order_status' => $orderStatus,
            'update_time'  => $updateTime
        ];
        return $this->request($data, $this->pushOrderStatus);
    }

    /**
     * 推送绑卡反馈
     * @param $orderNo
     * @param $bindStatus
     * @param $bindCardType
     * @param $reason
     * @return mixed|\Psr\Http\Message\ResponseInterface
     * @throws \App\Exceptions\GeneralException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @author luwei
     * @date 2019-02-23 14:40
     */
    public function pushBindCardResult($orderNo, $bindStatus, $bindCardType, $reason) {
        $data = [
            'order_no'       => $orderNo,
            'bind_status'    => $bindStatus,
            'bind_card_type' => $bindCardType,
            'reason'         => $reason
        ];
        return $this->request($data, $this->pushBindCardResult);
    }

    /**
     * @param $orderNo
     * @param $periodNos 还款期数
     * @param $repayStatus 还款状态 1成功 2失败
     * @param $repayPlace 还款触发方式 1主动还款
     * @param $successTime 还款成功必传
     * @param $remark 备注
     * @return mixed|\Psr\Http\Message\ResponseInterface
     * @throws \App\Exceptions\GeneralException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @author luwei
     * @date 2019-02-25 10:00
     */
    public function pushRepayResult($orderNo, $periodNos, $repayStatus, $repayPlace, $successTime, $remark) {
        $data = [
            'order_no'     => $orderNo,
            'period_nos'   => $periodNos,
            'repay_status' => $repayStatus,
            'repay_place'  => $repayPlace,
            'success_time' => $successTime,
            'remark'       => $remark
        ];
        return $this->request($data, $this->pushRepayResult);
    }

    /**
     * Description:推送审批通过结果
     * Author: Sam
     * DateTime: 2019-03-06 14:00
     * @param $order_no "授信trans_id"
     * @param $approval_time "授信通过时间"
     * @param $approval_term "授信真实期数"
     * @param $approval_amount "申请金额"
     * @param $pay_amount "还款总额"
     * @param $remark "总还款额组成"
     * @param $receive_amount "总到账金额"
     * @param $period_amount "首期应还金额"
     * @return mixed|\Psr\Http\Message\ResponseInterface
     * @throws \App\Exceptions\GeneralException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function pushLoanResultSuccess($order_no,$approval_time,$approval_term,$approval_amount,$pay_amount,$remark,$receive_amount,$period_amount){
        $data = [
            'order_no'=>$order_no,
            'conclusion'=>10,
            'approval_time'=>$approval_time,
            'amount_type'=>0,
            'approval_amount'=>$approval_amount,
            'term_unit'=>2,
            'term_type'=>0,
            'approval_term'=>$approval_term,
            'pay_amount'=>$pay_amount,
            'remark'=>$remark,
            'receive_amount'=>$receive_amount,
            'period_amount'=>$period_amount,
        ];
        return $this->request($data,$this->pushLoanResult);
    }

    /**
     * Description:推送审批拒绝结果
     * Author: Sam
     * DateTime: 2019-03-06 14:03
     * @param $order_no
     * @param $remark
     * @param $failed_time
     * @return mixed|\Psr\Http\Message\ResponseInterface
     * @throws \App\Exceptions\GeneralException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function  pushLoanResultFail($order_no,$remark,$failed_time){
        $data = [
            'order_no'=>$order_no,
            'conclusion'=>40,
            'remark'=>$remark,
            'refuse_time'=>$failed_time,
        ];
        return $this->request($data,$this->pushLoanResult);
    }

}